﻿using System;
using Fluent.Logging;
using OpenQA.Selenium;

namespace Fluent.HtmlModel
{
    public class TextBox<TPage> : HtmlControl<TPage>
        where TPage : WebPageBase
    {
        public TextBox(TPage page, Finder finder)
            : base(page, finder) { }

        public event Action<TextBox<TPage>, string> SetTextFunc;
        public event Action<TextBox<TPage>, string> OnSettingText;
        public event Action<TextBox<TPage>, string> OnSetText;

        public TPage SetText(string setTxt)
        {
            Log.Information("Set text: {0}", setTxt);
            if (SetTextFunc == null)
            {

                if (OnSettingText != null) OnSettingText(this, setTxt);

                this.WaitShow();
                this.ClearContent();
                this.DriverElement.SendKeys(setTxt);

                if (OnSetText != null) OnSetText(this, setTxt);
            }
            else
            {
                SetTextFunc(this, setTxt);
            }

            Log.Information("Then get the current value is: {0}", this.Value);
            return this.Page;
        }

        public TPage AppendText(string appendTxt)
        {
            string setTxt = this.Value + appendTxt;

            return this.SetText(setTxt);
        }
    }
}
